package com.gzbugu.bugusys.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.gzbugu.bugusys.util.HibernateUtils;
import com.gzbugu.bugusys.vo.Attendance;
import com.gzbugu.bugusys.vo.Staff;
import com.gzbugu.bugusys.vo.Type;
import com.gzbugu.bugusys.dao.IBaseDAO;

@Controller
public class AddAttendanceAction extends ActionSupport{
	
	@Resource(name="baseDAO")
	private IBaseDAO baseDAO;
	
	Type attendanceType = null ;
	private List errorList = null ; 
	Staff staff = null ;

	private Attendance attendance ;
	private Integer attendanceTypeId ;
	private String staffId ;
		
	public String execute(){
		String result="" ;
		Session session = null ;
		Transaction tx = null ;
		if(this.invalidate()){
			try {
				session = HibernateUtils.getSession() ;
				tx = session.beginTransaction() ;
				attendanceType = (Type) baseDAO.findById(this.attendanceTypeId.shortValue(),com.gzbugu.bugusys.fields.VoClasseFields.TYPE) ;
				attendance.setType(attendanceType) ;
				attendance.setStaff(staff);
				session.saveOrUpdate(attendance) ;
				tx.commit() ;
				result = "success";
			} catch (Exception e) {
				e.printStackTrace() ;
				tx.rollback(); 
			} finally{
				HibernateUtils.closeSession(session) ;
			}		
		}else{
			result = "cuowu" ;
		}
		return result ;
	}
	
	public boolean invalidate(){
		boolean flag = true ;
		errorList = new ArrayList() ;
		if("".equals(this.staffId)||this.staffId==null){
			this.errorList.add("员工编号不能为空！") ;
			flag = false ;
		}else{
			staff = (Staff) baseDAO.findById(this.staffId,
					com.gzbugu.bugusys.fields.VoClasseFields.STAFF) ;
			if(staff==null){
				this.errorList.add("编号为"+staffId+"的员工不存在！") ;
				flag = false ;			
			}
		}
		if(attendance.getStart()==null||"".equals(attendance.getStart())||attendance.getEnd()==null||"".equals(attendance.getEnd())){
			this.errorList.add("请填写考勤起止时间！") ;
			flag = false ;
		}else{
			if(attendance.getEnd().getTime()-attendance.getStart().getTime()<=0){
				this.errorList.add("起始时间有冲突！") ;
				flag = false ;
			}
		}
		
		if(this.attendanceTypeId==0){
			this.errorList.add("未选择考勤事件类型！") ;
			flag = false ;
		}
		return flag ;
	}
	
	public String getStaffId() {
		return staffId;
	}
	
	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}
	
	public Attendance getAttendance() {
		return attendance;
	}
	
	public void setAttendance(Attendance attendance) {
		this.attendance = attendance;
	}
	
	public Integer getAttendanceTypeId() {
		return attendanceTypeId;
	}

	public void setAttendanceTypeId(Integer attendanceTypeId) {
		this.attendanceTypeId = attendanceTypeId;
	}

	public List getErrorList() {
		return errorList;
	}

	public void setErrorList(List errorList) {
		this.errorList = errorList;
	}
	
}
